﻿Imports System.Data.SqlClient
Imports System.Data
Public Class frmBanDoTongThe

    Private Sub frmBanDoTongThe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Thiết lập chế độ hiện thị lớn nhất
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub CtrlTachThua_ChonHoSoTuThuaDat(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CtrlTachThua1.ChonHoSoTuThuaDat

        Dim strToBanDo As String = ""
        Dim strSoThua As String = ""
        Dim cls As New clsHoSoCapGCN
        Dim strCon As String = GetConnection(bolKetNoiCSDL)
        cls.Connection = strCon
        cls.ToBanDo = CtrlTachThua1.ToBanDo
        cls.SoThua = CtrlTachThua1.SoThua
        cls.MaThuaDat = CtrlTachThua1.MaSoThuaDat
        cls.MaDonViHanhChinh = User.DonViHanhChinhHienThoi
        Dim dt As New DataTable
        Dim dtMaThuaDat As New DataTable
        cls.SelectMaHoSoCapGCN(dt)
        cls.SelectMaThuaDatInBanDo(dtMaThuaDat)
        If (dt.Rows.Count > 0) Then
            If dt.Rows(0).Item("MaSoThuaDat").ToString = "" Then
                MessageBox.Show("Kiểm tra dữ liệu hồ sơ " & dt.Rows(0).Item("MaHoSoCapGCN").ToString)
                Exit Sub
            End If
            frmMain.HienThiThongTinThuaDat(dt.Rows(0).Item("MaHoSoCapGCN").ToString, dt.Rows(0).Item("MaSoThuaDat").ToString)
            With frmNghiepVuHoSo.CtrlNghiepVuHoSo1.CtrlThongTinThuaTuNhien1
                .Connection = strCon
                .MaDonViHanhChinh = User.DonViHanhChinhHienThoi

            End With

            frmNghiepVuHoSo.CtrlNghiepVuHoSo1.CtrlThongTinThuaTuNhien1.ShowData(dt.Rows(0).Item("MaSoThuaDat").ToString)
            strGlbMaThuaDat = dt.Rows(0).Item("MaSoThuaDat")
            frmMain.HienThiNghiepVu()
            frmNghiepVuHoSo.BringToFront()
 
        Else

            With dtMaThuaDat
                frmNghiepVuHoSo.Close()
                Dim frm As New frmNghiepVuHoSo
                frm.CtrlNghiepVuHoSo1.ToBanDo = .Rows(0).Item("ToBanDo")
                frm.CtrlNghiepVuHoSo1.SoHieuThua = .Rows(0).Item("SoThua")
                frm.CtrlNghiepVuHoSo1.MaThuaDat = .Rows(0).Item("MaSoThuaDat")
                frm.CtrlNghiepVuHoSo1.DienTichTuNhien = .Rows(0).Item("DienTichTuNhien")
                strGlbMaThuaDat = .Rows(0).Item("MaSoThuaDat")

                With frm.CtrlNghiepVuHoSo1.CtrlThongTinThuaTuNhien1
                    .Connection = strCon
                    .MaDonViHanhChinh = User.DonViHanhChinhHienThoi
                End With
                frm.CtrlNghiepVuHoSo1.ShowLandOriginalInformation()
                frm.CtrlNghiepVuHoSo1.activeTaskForm()
                frmMain.HienThiNghiepVu()
                frm.BringToFront()
            End With
        End If
    End Sub
    Private Sub frmBanDoTongThe_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
        Try
            Dim dlg As DialogResult = MessageBox.Show("Bạn có muốn thoát khỏi chức năng tách ghép không?", "Warning", MessageBoxButtons.YesNo)
            If dlg = DialogResult.Yes Then 
                Dim con As String = GetConnection(bolKetNoiCSDL)
                Dim connection As New SqlConnection(con)
                Dim Cmd As New SqlCommand("spDeleteThuaDatDangThaoTac", connection)
                Cmd.CommandType = CommandType.StoredProcedure

                Dim Para As New SqlParameter
                Para = Cmd.Parameters.Add("@MaThuaDat", SqlDbType.NVarChar, 50)
                Para.Value = CtrlTachThua1.MaThuaDatTam
                Para = Cmd.Parameters.Add("@MaDonViHanhChinh", SqlDbType.NVarChar, 50)
                Para.Value = CtrlTachThua1.MaDonViHanhChinh
                connection.Open()
                Cmd.ExecuteNonQuery()
                connection.Close()
                e.Cancel = False
                Me.CtrlTachThua1.FormClosing()
             
            Else : e.Cancel = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)

        End Try
    End Sub
End Class